package edu.ped.android.adapter;

import java.util.List;

import edu.ped.android.nfc.R;

import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ApplicationInfoAdapter extends ArrayAdapter<ResolveInfo> {

	ResolveInfo[] appsInfo ;
	Context context;
	PackageManager packageManager;

	  ViewHolder holder;
      Drawable icon;

      class ViewHolder {
              ImageView icon;
              TextView title;
      }
	
	public ApplicationInfoAdapter(Context context,
			final int textViewResourceId, List<ResolveInfo> objects) {
		super(context, textViewResourceId,objects);
		appsInfo = new ResolveInfo[1];
		this.appsInfo= objects.toArray(appsInfo);
		this.context = context;
		packageManager = context.getPackageManager();
	}

	public View getView(int position, View convertView,
			ViewGroup parent) {
		final LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(
						Context.LAYOUT_INFLATER_SERVICE);

		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.applicationadapterlayout, null);

			holder = new ViewHolder();
			holder.icon = (ImageView) convertView
					.findViewById(R.id.icon);
			holder.title = (TextView) convertView
					.findViewById(R.id.title);
			convertView.setTag(holder);
		} else {
			// view already defined, retrieve view holder
			holder = (ViewHolder) convertView.getTag();
		}              

		Drawable tile = packageManager.getApplicationIcon(appsInfo[position].activityInfo.applicationInfo);
		holder.title.setText(packageManager.getApplicationLabel(appsInfo[position].activityInfo.applicationInfo));
		holder.icon.setImageDrawable(tile);
		return convertView;
	}


}


